﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GQ.Controls.Tools;
using GQ.BLL.QuanLyKho;
using GQ.Utils;
using GQ.WinForms;

namespace GQ.QuanLyKho
{
    public partial class frmListKho : frmBase
    {
        public frmListKho()
        {
            InitializeComponent();
            GridUtils.SetStyle(gridView1);
        }

        private void frmListKho_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = Kho.LoadAll();
            //IList<ChiNhanh> chinhanhs = ChiNhanh.LoadAllBy("CompanyID={0}",Config.CompanyID);
            //foreach (ChiNhanh item in chinhanhs)
            //{
            //     repositoryItemImageComboBox1.Items.Add(new DevExpress.XtraEditors.Controls.ImageComboBoxItem(
            //          item.Ten,item.ID));
            //}
        }

        private void menuButton1_AddNewClick()
        {
            try
            {
                frmKho frm = new frmKho();
                frm.Kho = new Kho();
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    IList<Kho> khos = (IList<Kho>)gridControl1.DataSource;
                    khos.Add(frm.Kho);
                    Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);
                }
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);

            }
            menuButton1.Action = GQ.Controls.ActionState.View;
        }

        private void menuButton1_DeleteClick()
        {
            Kho kho = (Kho)gridView1.GetFocusedRow();
            if (kho == null)
            {               
                return;
            }
            try
            {
                if (Box.Show("Bạn có muốn xóa kho này không?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                  
                    return;
                }

                kho.Delete();
                IList<Kho> khos = (IList<Kho>)gridControl1.DataSource;
                khos.Remove(kho);
                Helpers.Project.SetStatus("Xóa thành công", StatusInfo.Complete);
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Xóa không thành công", StatusInfo.Error);

            }
        }

        private void menuButton1_EditClick()
        {
            Kho kho = (Kho)gridView1.GetFocusedRow();
            if (kho == null)
            {
                menuButton1.Action = GQ.Controls.ActionState.View;
                return;
            }
            try
            {
                frmKho frm = new frmKho();
                frm.Kho = kho;
                if (frm.ShowDialog(this) == DialogResult.Yes)
                    Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);

            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);

            }
            menuButton1.Action = GQ.Controls.ActionState.View;

        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            menuButton1_EditClick();
        }
    }
}
